package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FeedbackMapper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.Feedback;
import com.itheima.pojo.SysLog;
import com.itheima.pojo.User;
import com.itheima.service.FeedbackService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.util.*;

/**
 * @ClassName FeedbackServiceImpl
 * @Description 反馈表 服务实现类
 * @Author pengkaka
 * @Date 2020/8/6 14:27
 * @Version 1.0
 **/
@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private UserMapper userMapper;


    // 获取反馈列表
    @Override
    public Page<Feedback> getPageList(Map<String, Object> searchMap) {
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);

        Example example = new Example(Feedback.class);
        // 排序功能实现:字段名引号问题
        example.setOrderByClause("create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (searchMap.get("title") != null && !"".equals(searchMap.get("title"))) {
            criteria.andLike("title", "%" + searchMap.get("title") + "%");
        }
        if (searchMap.get("feedbackStatus") != null && !"".equals(searchMap.get("feedbackStatus"))) {
            criteria.andEqualTo("feedbackStatus", "%" + searchMap.get("feedbackStatus") + "%");
        }
        criteria.andEqualTo("status", "1");
        List<Feedback> feedbackList = feedbackMapper.selectByExample(example);

        // 设置反馈用户        
        feedbackList.forEach(feedback -> {
            if (StringUtils.isNotEmpty(feedback.getUserUid())) {
                String userUid = feedback.getUserUid();
                User user = userMapper.selectByPrimaryKey(userUid);
                user.setPassWord("");
                feedback.setUser(user);
            }
        });

        // 转换成page对象
        Page<Feedback> pageList = (Page<Feedback>) feedbackList;

        return pageList;
    }

    // 新增反馈
    @Override
    public void add(Feedback feedback) {
        String uid = UUID.randomUUID().toString().toLowerCase().replaceAll("-", "");
        feedback.setUid(uid);
        feedback.setStatus(true);
        feedback.setCreateTime(new Date());
        feedback.setUpdateTime(new Date());
        feedback.setFeedbackStatus(1);
        feedbackMapper.insert(feedback);
    }

    // 批量删除反馈
    @Override
    public void deleteBatch(List<Feedback> feedbackList) {
        if (feedbackList.size() <= 0) {
            throw new RuntimeException("参数错误");
        }
        feedbackList.forEach(feedback -> {
            feedback.setUpdateTime(new Date());
            feedback.setStatus(false);
            feedbackMapper.updateByPrimaryKeySelective(feedback);
        });
        
    }
}
